package com.lkm.app_ui.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.lkm.app_ui.FunHelp;
import com.lkm.app_ui.MBaseFragment;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.R;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.QRCodeUtil;
import com.lkm.comlib.Config;
import com.lkm.comlib.help.ImageViewLoadHelp;
import com.lkm.frame.otto.LinkBusCycleWrapper;
import com.lkm.frame.otto.Observer;
import com.lkm.frame.task2.CTaskExecutor;
import com.lkm.frame.task2.TaskAdapter;
import com.lkm.frame.util.CycleHelp;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.entity.PayTo;
import com.lkm.market2_0.help.SteelardHelp;
import com.lkm.market2_0.m.steelyarddata.OrderEntity;
import com.lkm.market2_0.m.steelyardserver.OnSteelyardKeyEvent;

import androidx.annotation.Nullable;




public class PayOutlineFragment extends MBaseFragment implements View.OnClickListener {

    public static PayOutlineFragment getInstance() {
        PayOutlineFragment f = new PayOutlineFragment();
        Bundle data = new Bundle();
        f.setArguments(data);
        return f;
    }

    @Override
    protected int configLayoutResID() {
        return R.layout.fragment_pay_outline;
    }

    private ImageViewLoadHelp payImageViewLoadHelp;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        payImageViewLoadHelp = new ImageViewLoadHelp(getActivity(), Math.max(getResources().getDisplayMetrics().heightPixels, getResources().getDisplayMetrics().widthPixels), holdCycleHelp());
        payImageViewLoadHelp.setHasImagScaleType(ImageView.ScaleType.CENTER_INSIDE);
        payImageViewLoadHelp.setLoadingImagScaleType(ImageView.ScaleType.CENTER_INSIDE);
        payImageViewLoadHelp.setNotImagScaleType(ImageView.ScaleType.CENTER_INSIDE);


        OrderEntity orderEntity = RunTimeData.orderEntity;

        ((TextView) findViewById(R.id.tv_totalPrice)).setText("应付金额：" + SteelardHelp.formatMoney(orderEntity.totalPrice) + "元");

//        if (StringHelp.isEmpty(RunTimeData.steelyardExtData.payOutLineInfo.wx)) {
//            findViewById(R.id.img_qr_wx).setVisibility(View.GONE);

//            payImageViewLoadHelp.setImage(((ImageView) findViewById(R.id.img_qr_wx)), RunTimeData.steelyardExtData.payOutLineInfo.wx);

        showImage();
        showPayItem();

        reOnKeyEvent();


        if(RunTimeData.isSongjian){
            onClickPay();
        }

    }


    private void showImage(){
        if (StringHelp.isEmpty(RunTimeData.steelyardExtData.payOutLineInfo.outlinePayImg)) {
            findViewById(R.id.img_qr_ali).setVisibility(View.GONE);
        } else
            payImageViewLoadHelp.setImage(((ImageView) findViewById(R.id.img_qr_ali)), RunTimeData.steelyardExtData.payOutLineInfo.outlinePayImg);
    }

    private void showPayItem() {
        if (!CollectionHelp.isEmpty(RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems)) {
            findViewById(R.id.img_qr_ali).setVisibility(View.GONE);
            findViewById(R.id.v_pay).setVisibility(View.VISIBLE);
            findViewById(R.id.v_pay_1).setVisibility(View.GONE);
            findViewById(R.id.v_pay_2).setVisibility(View.GONE);

            int size = Math.min(RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.size(), 2);
            for (int i = 0; i < size; i++) {
                PayTo.PayOutlineItemTo payOutlineItem = RunTimeData.steelyardExtData.payOutLineInfo.payOutlineItems.get(i);
                ViewGroup viewGroup = i == 0 ? findViewById(R.id.v_pay_1) : findViewById(R.id.v_pay_2);
                viewGroup.setVisibility(View.VISIBLE);
                ImageView img_icon = viewGroup.findViewWithTag("img_icon");
                TextView tv_title = viewGroup.findViewWithTag("tv_title");
                ImageView img_qr = viewGroup.findViewWithTag("img_qr");

                tv_title.setText(payOutlineItem.title);
                img_icon.setImageResource(R.mipmap.ic_pay_yd);
                if (payOutlineItem.title == null) {

                } else if (payOutlineItem.title.contains("微信")) {
                    img_icon.setImageResource(R.mipmap.ic_pay_wx);

                } else if (payOutlineItem.title.contains("支付宝")) {
                    img_icon.setImageResource(R.mipmap.ic_pay_ali);
                }
                setImage(img_qr, payOutlineItem.url);

            }

        } else {
            findViewById(R.id.v_pay).setVisibility(View.GONE);
        }


    }

    private void reOnKeyEvent() {
        LinkBusCycleWrapper.register(new Observer<OnSteelyardKeyEvent>() {
            @Override
            public Class<OnSteelyardKeyEvent> getBusEventClass() {
                return OnSteelyardKeyEvent.class;
            }

            @Override
            public void onReceiver(OnSteelyardKeyEvent data, Runnable nextRun) {
                try {
                    if (data.isIntercepted()) return;
                    data.setIntercepted(true);

                    View v = getView().findViewWithTag("fun_" + data.getKeyCode());
                    if (v != null)
                        onClick(v);

                }catch (Exception e){

                }finally {
                    nextRun.run();
                }
            }
        }, holdCycleHelp(), CycleHelp.CycleType.Destroy);
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_pay_yj) {
            onClickPay();
//        } if (v.getId() == R.id.tv_pay_yj1) {

        } else if (v.getId() == R.id.tv_pay_qx) {
            onClickPayCancel();
        }
    }

    private void onClickPay() {

        FunHelp.quickPay();
        RouterUINavigation.getInstance().dismiss(this);
    }

    private void onClickPayCancel() {
        RouterUINavigation.getInstance().dismiss(this);
    }





//            FunModule steelyardServerModule = AppConst.getModuleManager().loadModule(FunModule.class);








//            FunModule steelyardServerModule = AppConst.getModuleManager().loadModule(FunModule.class);





    @Override
    protected boolean configIsLockKey() {
        return true;
    }

    public void setImage(ImageView image,String s){
        CreateQR createQR = new CreateQR(image);
        createQR.setParam(new Object[]{s});
        createQR.action();
        createQR.execTask();
    }
    protected class CreateQR extends TaskAdapter<Object[], Void, Bitmap> {
        ImageView img_qr;
        public CreateQR(ImageView img_qr) {
            super(new CTaskExecutor<Object[], Void, Bitmap>(Config.getTaskExecutor(), Looper.getMainLooper()), PaySuccessTipsFragment.CreateQR.class.getName(), MyApplication.getInstance().getTaskManager2());
            this.img_qr=img_qr;
        }
        @Override
        public void doExecuting(Object[] param) {
            super.doExecuting(param);
            mTaskExecutor.executorDoAsyncExecuting(new RunnablePR<Object[], Void>() {
                @Override
                public Void run(Object[] objects) {
                    complete(onExecuting(objects));
                    return null;
                }
            }, param);
        }
        public Bitmap onExecuting(Object[] param) {
            int ip = -1;
            String orderurl = (String) param[++ip];
            Bitmap bitmap = QRCodeUtil.createImage(orderurl, 400, 400);
            return bitmap;
        }
        @Override
        public void onFinish(Bitmap bitmap) {
            super.onFinish(bitmap);
            if (isExit()) return;
            try {
                img_qr.setImageBitmap(bitmap);
            }catch (Exception e){}

        }
    }
}
